package com.chookss.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

import com.chookss.tools.Utils;


public class ProgressBarView extends View {

    private Paint mPaintBack;
    private Paint mPaint;
    private float process;
    private int strokeWidth = 15;
    private int textSize = 20;
    private long duration = 3000;
    private float startDegree = 0;
    private float endDegree = 360;
    private float maxValue = 100;
    private String text = "完成";
    private String defaultText = "0%";
    private ValueAnimator valueAnimator;

    public ProgressBarView(Context context) {
        super(context);
        init();
    }

    public ProgressBarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ProgressBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaintBack = new Paint();
        mPaintBack.setColor(Color.parseColor("#00000000"));
        mPaintBack.setStyle(Paint.Style.STROKE);
        mPaintBack.setAntiAlias(true);
        mPaintBack.setStrokeCap(Paint.Cap.ROUND);
        mPaintBack.setStrokeWidth(strokeWidth);

        mPaint = new Paint();
        mPaint.setColor(Color.parseColor("#F12112"));
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(strokeWidth);
    }

    public void setStrokeWidth(int width) {
        strokeWidth = width;
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //创建圆环矩形
        RectF rectF = new RectF(strokeWidth / 2, strokeWidth / 2, Utils.dip2px(getContext(), 75) - strokeWidth / 2, Utils.dip2px(getContext(), 75) - strokeWidth / 2);
        //画出灰色进度条作为背景
        canvas.drawArc(rectF, -90, 360, false, mPaintBack);
        //画进度条
        canvas.drawArc(rectF, -90, process, false, mPaint);
    }

    /**
     * 设置动画效果
     */
    public void start() {
        valueAnimator = ValueAnimator.ofFloat(startDegree, endDegree);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(animation -> {
            process = (float) animation.getAnimatedValue();
            invalidate();
        });
        valueAnimator.setDuration(duration);
        valueAnimator.start();
    }

    /**
     * 设置动画效果
     */
    public void stop() {
        try {
            if (valueAnimator != null) {
                valueAnimator.removeAllUpdateListeners();
                valueAnimator.cancel();
                valueAnimator=null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 设置动画效果
     */
    public void pause() {
        try {
            if (valueAnimator != null) {
                valueAnimator.pause();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 设置动画效果
     */
    public void resume() {
        try {
            if (valueAnimator != null) {
                valueAnimator.resume();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private int sp2px(int sp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
                getResources().getDisplayMetrics());
    }
}